# File Information --------------------------------------------------------

# Clear R -----------------------------------------------------------------
  rm(list = ls())

# Packages ----------------------------------------------------------------
  library(ggplot2)
  library(dplyr)
  library(xtable)
  library(ggplot2)
  library(cowplot)
  library(reshape)

# Working Directory -------------------------------------------------------
  setwd("~/Replication Materials for The Domestic Economic Costs of Sanctions - A Firm Level Analysis/")
  list.files()


# Create Data Sets for Figures --------------------------------------------

  # Firm Names
    firms2 <- rbind("Apple", "Apple", "Dell", "Dell", "Dow Chemical","Dow Chemical", "PPG", "PPG", "Walmart", "Walmart", "Duke", "Duke", "Deere", "Deere")
  
    firms3 <- rbind("Colgate-Palmolive", "Kimberly-Clark", "Exxon Mobil", "Haliburton", "Ford", "TJX Companies", "Disney", "AT&T Corp")
  
    firms4 <- rbind("Advanced Micro Devices", "Analog Devices", "Intel Corporation", "Maxim Integrated", "Microchip Technology Inc.", "Micron Technology Inc.",
                  "Qualcomm", "Texas Instruments", "Xilinx", "XXX", "Allergan", "Johnson & Johnson", "Eli Lilly & Co.", "XXX", "Merck & Co.", "Perrigo", "Pfizer", "XXX")
  
  # Tickers
    tickers2 <- rbind("AAPL", "AAPL", "Dell", "Dell", "DD", "DD", "PPG", "PPG", "WMT", "WMT", "DUKE", "DUKE", "DE", "DE")
    
    tickers3 <- rbind("CL", "KMB", "XOM", "HAL", "F", "TJX", "DIS", "T")
    
    tickers4 <- rbind("AMD", "ADI", "INTC", "MXIM", "MCHP", "MU", "QCOM", "TXN", "XLNX", "XXX", "AGN", "JNJ", "LLY", "XXX", "MRK", "PRGO", "PFE", "XXX")

  # Year
    years2 <- rbind(1989, 2011, 1989, 2011, 1989, 2011, 1989, 2011, 1989, 2011, 1989, 2011, 1989, 2011)
    
    years3 <- rbind(1993,1993, 1992,1992, 1991,1991, 1997,1997)
    
    years4 <- rbind(1997,1997,1997,1997,1997,1997,1997,1997,1997,1111,1999,1999,1999,1111,1999,1999,1999,1111)

  # Coefficients
    coefs2 <- rbind(0.103, 0.634, -0.063, 0.945, 0.363, 1.232, 0.378, 0.734, 0.094, 0.426, -0.176, 0.455, -0.197, 0.797)
    
    coefs3 <- rbind(0.201,-0.024, 0.430, 0.248, 0.799, 0.328, 0.671, 0.145)
    
    coefs4 <- rbind(0.789, 0.517, 0.552, 0.534, 0.325, 0.261, 0.634, 0.429, 0.848, -1.111 ,0.497, 0.893, 0.621, -1.111, 0.588, 0.634, 0.614, -1.111)

  # Standard Error 
    se2 <- rbind(0.103, 0.221, 0.142, 0.156, 0.180, 0.422, 0.160, 0.265, 0.165, 0.195, 0.157, 0.236, 0.154, 0.239)
    
    se3    <- rbind(0.088, 0.100, 0.124, 0.56, 0.353, 0.264, 0.181, 0.198)
    
    se4    <- rbind(0.157, 0.150, 0.133, 0.262, 0.147, 0.211, 0.156, 0.202, 0.175, 0.111, 0.319, 0.247, 0.191, 0.111, 0.297, 0.488, 0.221, 0.111)

  # Z
    z2 <- round(coefs2/se2)
    
    z3 <- round(coefs3/se3,3)
    
    z4 <- round(coefs4/se4,3)

# Organizational Inputs for Data Sets -------------------------------------

  # Figure 2
    
    # Create Data Set
      interested <- rbind(0,1,0,1,1,1,1,1,0,1,0,1,0,1)
      fig2dat <- data.frame(cbind(years2, coefs2, se2, z2, interested))
      fig2dat$comp <- rbind("wsot1", "wsot1", "wsot1", "wsot1", "wsot2", "wsot2", "wsot2", "wsot2", "asot", "asot", "asot", "asot", "asot", "asot")
      
      colnames(fig2dat) <- c("years","coefs","se","z","interested","comp")

      fig2dat$firms <- firms2
      fig2dat$tickers <- tickers2

  # Figure 3
    target3 <- rbind("Romania 1993","Romania 1993", "Columbia 1992","Columbia 1992", "Germany 1991","Germany 1991", "France 1997","France 1997")  
    
    home3 <- rbind("US","US", "US","US", "US","US", "US","US")
    
    episode3 <- rbind("Sanction","Sanction", "Sanction","Sanction", "Sanction Threat", "Sanction Threat", "Sanction Threat", "Sanction Threat")
      
    interested3 <- rbind("Interests", "No Interests", "Interests", "No Interests", "Interests", "No Interests", "Interests", "No Interests")
    
    fig3dat <- data.frame(cbind(episode3,target3,years3,firms3,tickers3,home3,interested3,coefs3,se3,z3))
    colnames(fig3dat) <- c("episode","target","year","firms","tickers","homes","interests","beta","se","z")
    head(fig3dat)
    
    fig3dat$beta <- coefs3
    fig3dat$se <- se3
    fig3dat$z <- z3
    
  # Figure 4
    target4 <- rbind("Imposed Sanctions against Japan 1997",
                     "Imposed Sanctions against Japan 1997",
                     "Imposed Sanctions against Japan 1997",
                     "Imposed Sanctions against Japan 1997",
                     "Imposed Sanctions against Japan 1997",
                     "Imposed Sanctions against Japan 1997",
                     "Imposed Sanctions against Japan 1997",
                     "Imposed Sanctions against Japan 1997",
                     "Imposed Sanctions against Japan 1997",
                     "Sanctions Threats against Spain 1999",
                     "Sanctions Threats against Spain 1999",
                     "Sanctions Threats against Spain 1999",
                     "Sanctions Threats against Spain 1999",
                     "Sanctions Threats against Spain 1999",
                     "Sanctions Threats against Spain 1999",
                     "Sanctions Threats against Spain 1999",
                     "Sanctions Threats against Spain 1999",
                     "Sanctions Threats against Spain 1999")
    
    home4 <- rbind("US","US","US","US","US","US","US","US","US",
                   "XXX","Ireland","US","US","XXX","US","Ireland","US","XXX")
    
    episode4 <- rbind("Sanction","Sanction","Sanction","Sanction","Sanction","Sanction","Sanction","Sanction","Sanction",
                      "Sanction Threat","Sanction Threat","Sanction Threat","Sanction Threat","Sanction Threat","Sanction Threat",
                      "Sanction Threat","Sanction Threat","Sanction Threat")
    
    fig4dat <- data.frame(cbind(episode4,target4,years4,firms4,tickers4,home4,coefs4,se4,z4))
    colnames(fig4dat) <- c("episode","target","year","firms","tickers","homes","beta","se","z")
    head(fig4dat)
    
    fig4dat$beta <- coefs4
    fig4dat$se <- se4
    fig4dat$z <- z4
    
    fig4dat$xaxis <- c(1:9,1:9)
    
# Figure 2 ----------------------------------------------------------------

  # Within Sectors Over Time
    wsot_dat <- data.frame(subset(fig2dat, fig2dat$comp == "wsot1" | fig2dat$comp == "wsot2"))  
    
    wsot_dat$count <- 1:nrow(wsot_dat)

    asot_dat <- data.frame(subset(fig2dat, fig2dat$comp == "asot"))    
    
    asot_dat$count <- 1:nrow(asot_dat)

    wsot <- ggplot(wsot_dat, aes(count,z, colour = factor(interested))) +
            theme_bw() +
            scale_colour_manual(values=c("gray80", "gray30")) +
            ggtitle("Within Sectors Over Time") +
            geom_text(size = 8, aes(label= wsot_dat$tickers)) +
            theme(plot.title = element_text(size = 30, face = "bold", hjust = 0.5)) +
            theme(axis.title = element_text(size = 30)) +
            theme(axis.text = element_text(size = 15)) +
            theme(plot.margin=unit(c(t = 0.5, r = 1, b =0.25, l =1.2),"cm")) +
            theme(axis.text.x = element_text(angle = 0, hjust = 0.5, size = 25)) +
            theme(axis.text.y = element_text(angle = 0, hjust = 0.5, size = 25)) +
            theme(legend.position="none") +
            labs(x = "", y = "t-Sanctions \n ") +
            geom_hline(yintercept = 1.68, linetype = "dashed", color = "black", size = 2) +
            scale_x_discrete(limits = wsot_dat$count, labels = c("1989", "2011",
                                                                 "1989", "2011",
                                                                 "1989", "2011",
                                                                 "1989", "2011"), position = "bottom")
    wsot

  # Across Sectors Over Time
    asot <- ggplot(asot_dat, aes(count,z, colour = factor(interested))) +
      theme_bw() +
      scale_colour_manual(values=c("gray80", "gray30")) +
      ggtitle("Across Sectors Over Time") +
      geom_text(size = 8, aes(label= asot_dat$tickers)) +
      theme(plot.title = element_text(size = 30, face = "bold", hjust = 0.5)) +
      theme(axis.title = element_text(size = 30)) +
      theme(axis.text = element_text(size = 15)) +
      theme(plot.margin=unit(c(t = 0.5, r = 1, b = 1.2, l =1.2),"cm")) +
      theme(axis.text.x = element_text(angle = 0, hjust = 0.5, size = 25)) +
      theme(axis.text.y = element_text(angle = 0, hjust = 0.5, size = 25)) +
      theme(legend.position="none") +
      labs(x = "\n Years", y = "t-Sanctions \n ") +
      geom_hline(yintercept = 1.68, linetype = "dashed", color = "black", size = 2) +
      scale_x_discrete(limits = asot_dat$count, labels = c("1989", "2011",
                                                           "1989", "2011",
                                                           "1989", "2011"), position = "bottom")
    asot

  # Legend
    a <- rbind("Yes","No")
    b <- rbind(1, 0)    
    c <- rbind(rnorm(2))

    dat <- data.frame(a,b,c)

    legendplot <- ggplot(dat, aes(b,X1, color = factor(a),
                                  stroke = 10)) +
      geom_point() +
      theme_bw() +
      theme(legend.position="bottom") +
      scale_colour_manual(values=c("gray80", "gray30")) +
      theme(legend.title = element_text(size = 30)) +
      theme(legend.text = element_text(size = 24)) +
      guides(color = guide_legend(override.aes = list(shape = 15, size = 15))) +
      labs(x = "\n Years", y = "t-Statistic for Sanctions Coefficients \n ", colour = "Commercial Interests in Target State:") 

    legend <- get_legend(legendplot)

    myplot <- plot_grid(wsot,asot,legend, ncol = 1, rel_heights = c(2,2,.175))
    myplot

    setwd("/Users/claywebb/Dropbox/KU/Research/Sanctions/The Domestic Economic Costs of Sanctions - A Firm Level Analysis/Manuscript/")
    list.files()
    ggsave("fig2.pdf", width = 26, height = 22)
    

# Figure 3 ----------------------------------------------------------------

  fig3 <- ggplot(fig3dat, aes(x = interests, y = z, colour = factor(interests))) +
    ylim(-0.5,4.5) + 
    facet_wrap(~episode + target, ncol = 4) +
    theme_bw() +
    scale_colour_manual(values=c("gray30", "gray80")) +
    geom_text(size = 6, aes(label= tickers)) +
    theme(plot.title = element_text(size = 20, face = "bold", hjust = 0.5)) +
    theme(axis.title = element_text(size = 20)) +
    theme(axis.text = element_text(size = 15)) +
    theme(plot.margin=unit(c(t = 0.5, r = 1, b = 1.2, l =1.2),"cm")) +
    theme(axis.text.x = element_text(angle = 0, hjust = 0.5, size = 20)) +
    theme(axis.text.y = element_text(angle = 0, hjust = 0.5, size = 20)) +
    theme(strip.text.x = element_text(size = 20)) +
    theme(legend.position="none") +
    labs(x = "\n Commercial Interests in Target State", y = "t-Sanctions \n ") +
    geom_hline(yintercept = 1.68, linetype = "dashed", color = "black", size = 2)
  
  fig3    
  
  ggsave("fig3.pdf", width = 18, height = 6)


# Figure 4 ----------------------------------------------------------------

  fig4 <- ggplot(fig4dat, aes(x = xaxis, y = z, colour = factor(homes))) +
    coord_cartesian(ylim=c(-.5,6)) + 
    theme_bw() +
    geom_text(size = 6, aes(label= tickers)) +
    facet_wrap(~ target, ncol = 1)  +
    scale_colour_manual(values=c("gray80", "gray30","black")) +
    geom_text(size = 6, aes(label= tickers)) +
    theme(plot.title = element_text(size = 20, face = "bold", hjust = 0.5)) +
    theme(axis.title = element_text(size = 20)) +
    theme(axis.text = element_text(size = 15)) +
    theme(plot.margin=unit(c(t = 0.5, r = 1, b = 0.005, l =1.2),"cm")) +
    theme(axis.text.x=element_blank()) +
    theme(axis.ticks.x=element_blank()) +
    theme(axis.text.y = element_text(angle = 0, hjust = 0.5, size = 20)) +
    theme(strip.text.x = element_text(size = 20)) +
    theme(legend.position="none") +
    labs(x = "", y = "t-Sanctions \n ") +
    geom_hline(yintercept = 1.68, linetype = "dashed", color = "black", size = 2)
  
  fig4   
  
  legendplot <- ggplot(dat, aes(b,X1, color = factor(a),
                                stroke = 10)) +
    geom_point() +
    theme_bw() +
    theme(legend.position="bottom") +
    scale_colour_manual(values=c("gray80", "gray30")) +
    theme(legend.title = element_text(size = 20)) +
    theme(legend.text = element_text(size = 20)) +
    guides(color = guide_legend(override.aes = list(shape = 15, size = 15))) +
    theme(plot.margin=unit(c(t = .005, r = 1, b = 0.005, l =1.2),"cm")) +
    labs(x = "\n Years", y = "t-Statistic for Sanctions Coefficients \n ", colour = "United States Firms:") 
  
  legend <- get_legend(legendplot)
  
  fig4_legend <- plot_grid(fig4,legend, ncol = 1, rel_heights = c(4,.75))
  fig4_legend
  
  ggsave("fig4.pdf", width = 18, height = 9)
  